﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Microsoft.Practices.Unity;
using System.Reflection;

namespace Wicresoft.UnifyShow.Portal.Controllers
{
    public class ITBusinessStatisticsController : BaseController
    {
        private ISiteBranch siteBranch;
        private IUser_X user;
        private IBusinessSystem businessSystem;

        [InjectionConstructor]
        public ITBusinessStatisticsController(ISiteBranch siteBranch, IUser_X user, IBusinessSystem businessSystem)
        {
            this.siteBranch = siteBranch;
            this.user = user;
            this.businessSystem = businessSystem;
        }
        protected override void Dispose(bool disposing)
        {
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)user).Dispose();
            ((IDisposable)businessSystem).Dispose();
        }

        public ActionResult Index(int businessSystemId)
        {
            base.SetRoles(user);
            var bSystem = businessSystem.GetBusinessSystemByID(businessSystemId);
            //权限控制
            if (!UserSiteIDs.Contains(bSystem.SiteID)) return new HttpStatusCodeResult(403);

            List<SiteBranchModel> sites = siteBranch.GetSiteALLBranch().ToList();
            var sitesContainsTheBS = businessSystem.GetSiteIDsContainsTheBusinessSystem(bSystem.Name);

            List<decimal> siteIDsContainsTheBS = sitesContainsTheBS.Select(o => o.SiteID).ToList();

            ViewBag.Sites = (from c in sites
                             where UserSiteIDs.Contains(c.ID) && siteIDsContainsTheBS.Contains(c.ID)
                             select c).ToList();

            ViewBag.SiteID = bSystem.SiteID;

            //ViewData["ServiceName"] = bSystem.Name;
            ViewData["ServiceAlias"] = bSystem.Alias;
            ViewData["ServiceID"] = bSystem.ID;
            ViewData["SitesContainsTheBS"] = sitesContainsTheBS;
            return View();
        }

        public ActionResult GetCharts()
        {
            String[] methods;
            Type incr = typeof(IncidentController);
            String[] inmethods = incr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Incident").ToArray();
            Type sdcr = typeof(ServiceDeskController);
            String[] sdmethods = sdcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&ServiceDesk").ToArray();
            Type chcr = typeof(ChangeController);
            String[] chmethods = chcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Change").ToArray();
            Type prcr = typeof(ProblemController);
            String[] prmethods = prcr.GetMethods().Where(p => p.ReturnType.FullName == "System.Web.Mvc.ActionResult").Select(n => n.Name + "&Problem").ToArray();
     
            methods = new String[inmethods.Length +sdmethods.Length + chmethods.Length + prmethods.Length];
            inmethods.CopyTo(methods, 0);
            sdmethods.CopyTo(methods, inmethods.Length);
            chmethods.CopyTo(methods, inmethods.Length + sdmethods.Length);
            prmethods.CopyTo(methods, inmethods.Length + sdmethods.Length + chmethods.Length);

            return PartialView(methods);
        }
    }
}
